import {
  DarkTheme,
  DefaultTheme,
  NavigationContainer,
  ThemeProvider,
} from '@react-navigation/native';
import { useFonts } from 'expo-font';
import { Stack, router, useSegments } from 'expo-router';
import * as SplashScreen from 'expo-splash-screen';
import { useEffect, useState } from 'react';
import 'react-native-reanimated';
import { Provider } from 'react-redux';
import store from '@/redux/index';
import { useColorScheme } from '@/hooks/useColorScheme';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { FFmpegKitConfig } from 'ffmpeg-kit-react-native';
// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();

export default function RootLayout() {
  const colorScheme = useColorScheme();
  const [loaded] = useFonts({
    SpaceMono: require('@/assets/fonts/SpaceMono-Regular.ttf'),
  });
  // useEffect(() => {
  //   FFmpegKitConfig.init()
  //     .then(() => {
  //       console.log('FFmpegKit initialized');
  //     })
  //     .catch((error) => {
  //       console.error('Error initializing FFmpegKit:', error);
  //     });
  // }, []);
  useEffect(() => {
    if (loaded) {
      SplashScreen.hideAsync();
    }
  }, [loaded]);

  if (!loaded) {
    return null;
  }

  // const [userInfo, setUserInfo] = useState(null);
  // const segments = useSegments();

  // useEffect(() => {
  //   const fetchUserInfo = async () => {
  //     const user:any = await AsyncStorage.getItem('userInfo');
  //     setUserInfo(user);
  //   };
  //   fetchUserInfo();
  // }, [segments]);

  // const protectedRoutes = ['personal', 'concern', 'message', 'explore'];
  // const isProtectedRoute = protectedRoutes.some((route) => segments.includes(route));
  // const shouldRedirect = isProtectedRoute && !userInfo;

  // useEffect(() => {
  //   if (shouldRedirect) {
  //     router.push('/login/Login');
  //   }
  // }, [shouldRedirect]);
  return (
    <Provider store={store}>
      <ThemeProvider value={colorScheme === 'dark' ? DarkTheme : DefaultTheme}>
        <Stack>
          <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
          <Stack.Screen name="login" options={{ headerShown: false }} />
          <Stack.Screen name="concernM" options={{ headerShown: false }} />
          <Stack.Screen name="chat" options={{ headerShown: false }} />
          <Stack.Screen name="home" options={{ headerShown: false }} />
          <Stack.Screen name="map" options={{ headerShown: false }} />
          <Stack.Screen name="personal" options={{ headerShown: false }} />
          <Stack.Screen name="+not-found" />
        </Stack>
      </ThemeProvider>
    </Provider>
  );
}
